package com.jlpay.openapi.demo.ledger.notify;

import com.jlpay.open.jlpay.sdk.java.enums.SignType;
import com.jlpay.open.jlpay.sdk.java.exception.SignVerifyException;
import com.jlpay.open.jlpay.sdk.java.http.HttpMethod;
import com.jlpay.open.jlpay.sdk.java.model.ledger.notify.MerchIssueResultNotifyRequest;
import com.jlpay.open.jlpay.sdk.java.model.notify.NotifyParam;
import com.jlpay.openapi.demo.JlpayOpenApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author chenjunhong
 * @since 2024/3/26
 */
public class LedgerNotifyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LedgerNotifyService.class);

    public static void main(String[] args) {
        // 替换为请求报文json
        String body = "{}";
        // 替换为回调接口uri
        String uri = "/notify";
        // 对应响应头x-jlpay-sign-alg
        String alg = SignType.SM3_WITH_SM2_WITH_DER.getCode();
        // 对应响应头x-jlpay-sign
        String sign = "MEYCIQCpHAZPdXEVbiGW17x9hY0JuEJ4eIX6Uo7Uxk9vczQk1wIhAPXsJHBVRQLiXTUFz15I8UHNLEcgWDwdDx5Uv8GN8Cuo";
        // 对应响应头x-jlpay-nonce
        String nonce = "QpPPCyWZFlgL1dI4MHJHVY37hqMN5YGG";
        // 对应响应头x-jlpay-timestamp
        String timestamp = "1711536850";
        NotifyParam notifyParam = NotifyParam.builder()
                .method(HttpMethod.POST.name())
                .uri(uri)
                .alg(alg)
                .sign(sign)
                .nonce(nonce)
                .timestamp(timestamp)
                .body(body)
                .build();
        // 出账方注册、修改回调验签
        MerchIssueResultNotifyRequest notifyRequest = null;
        try {
            notifyRequest = JlpayOpenApiService.notifyParseService().parse(notifyParam, MerchIssueResultNotifyRequest.class);
        } catch (SignVerifyException e) {
            // 签名验证失败
            LOGGER.error("签名验证失败", e);
        }
        // 业务逻辑处理

        // 返回回调处理结果
        // BaseResponse response = new BaseResponse("00", "成功");
    }

}
